﻿using Juli.Mes.Ladles.Dtos;
using Juli.Mes.Plans.Dtos;
using Lion.AbpPro.Core;
using Microsoft.AspNetCore.Mvc;
using Swashbuckle.AspNetCore.Annotations;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Identity;
using Volo.Abp.ObjectMapping;

namespace Juli.Mes.Plans
{
    [Route("Plans")]
    [Authorize]
    public class PlanAppService : MesAppService, IPlanAppService
    {
        private readonly IRepository<Plan> planRepository;
        private readonly IRepository<IdentityUser> userRepository;

        public PlanAppService(IRepository<Plan> planRepository,
             IRepository<IdentityUser> userRepository)
        {
            this.planRepository = planRepository;
            this.userRepository = userRepository;
        }

        [HttpPost("create")]
        [SwaggerOperation("新增计划", Tags = new[] { "Plans" })]
        public async Task CreatePlan(PlanInputDto input)
        {
            var entity = ObjectMapper.Map<PlanInputDto, Plan>(input);
            await planRepository.InsertAsync(entity);
        }

        [HttpPost("delete")]
        [SwaggerOperation("删除计划", Tags = new[] { "Plans" })]
        public async Task DeletePlan(IdInput input)
        {
            await planRepository.DeleteAsync(p => p.Id == input.Id);
        }

        [HttpPost("edit")]
        [SwaggerOperation("编辑计划", Tags = new[] { "Plans" })]
        public async Task EditPlan(PlanInputDto input)
        {
            var entity = await planRepository.GetAsync(p => p.Id == input.Id);
            ObjectMapper.Map(input, entity);
            await planRepository.UpdateAsync(entity);
        }

        [HttpPost("page")]
        [SwaggerOperation("分页查询计划", Tags = new[] { "Plans" })]
        public async Task<PagedResultDto<PlanOutputDto>> ListPlan(PagePlanInputDto input)
        {
            var stoveQuery = await planRepository.GetQueryableAsync();
            var userQuery = await userRepository.GetQueryableAsync();

            var query = from s in stoveQuery
                        join u1 in userQuery on s.CreatorId equals u1.Id
                        join u2 in userQuery on s.LastModifierId equals u2.Id into u22
                        from u23 in u22.DefaultIfEmpty()
                        select new PlanOutputDto()
                        {
                            Id = s.Id,
                            PlanSerialNum = s.PlanSerialNum,
                            SmeltNo = s.SmeltNo,
                            SteelType = s.SteelType,
                            State = s.State,
                            PlanStartTime = s.PlanStartTime,
                            PlanEndTime = s.PlanEndTime,
                            RealStartTime = s.RealStartTime,
                            RealEndTime = s.RealEndTime,
                            SmeltingProcess = s.SmeltingProcess,
                            TappingTemperature = s.TappingTemperature,
                            SpecialProcessRequirement = s.SpecialProcessRequirement,
                            CreationTime = s.CreationTime,
                            LastModificationTime = s.LastModificationTime,
                            CreatorName = u1.Name,
                            LastModifierName = u23.Name,
                        };

            //query = query.WhereIf(!string.IsNullOrEmpty(input.Filter),
            //    p => p.LadleCode.Contains(input.Filter)
            //    || p.LadleName.Contains(input.Filter)
            //    || p.Rfid.Contains(input.Filter));

            var items = query
                .OrderBy(e => e.CreationTime)
                .PageBy(input.SkipCount, input.PageSize)
                .ToList();

            var count = query.Count();
            return new PagedResultDto<PlanOutputDto>()
            {
                Items = items,
                TotalCount = count,
            };
        }
    }
}
